package main.java.com.yc.navy.snacknet.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.snacknet.bean.AddrInfo;
import com.yc.navy.snacknet.bean.MemberInfo;
import com.yc.navy.snacknet.enums.ResultEnum;
import com.yc.navy.snacknet.service.IAddrInfoService;
import com.yc.navy.snacknet.util.ConstantIfo;
import com.yc.navy.snacknet.vo.ResultVO;

@RestController
@RequestMapping("/addr")
public class AddrInfoController {
	@Autowired
	private IAddrInfoService addrInfoService;
	
	/**
	 * 查询当前登录用户的购物车信息
	 * @param session
	 * @return
	 */
	
	@GetMapping("/finds")
	public ResultVO finds(HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		List<AddrInfo> list = addrInfoService.finds(mf.getMno());
		if(list != null && !list.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, list);
		}
		return new ResultVO(ResultEnum.DATA_NULL);	
	}
	

	
	@PostMapping("/add")
	public ResultVO add(AddrInfo af, HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		MemberInfo mf = (MemberInfo) obj;
		String ano = UUID.randomUUID().toString();
		af.setAno(ano);
		af.setMno(mf.getMno());
		int result = addrInfoService.add(af);
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS, ano);
		}
		return new ResultVO(ResultEnum.ERROP);		
	}
	
	
}
